package com.kinon.markett2.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.kinon.markett2.entity.ParkInfoBean;

import java.util.ArrayList;

public class ParkSqliteDBDao {
    private SQLiteDatabase db;

    public ParkSqliteDBDao(Context context) {
        super();
        ParkSqliteDBHelper helper = new ParkSqliteDBHelper(context, "parkinfo.db",
                null, 1);
        db = helper.getWritableDatabase();
    }

    public Long insertDb(String park_id, String owner_id, String parkname,
                         String abstrac, String province, String city, String district,
                         String location, String balance, String frozen_amount,
                         String pending_amount, String sp_amount, String status,
                         String created_at, String updated_at, String logo, String rolename,
                         String is_system_role, String user_id, String join_time) {
        ContentValues values = new ContentValues();
        // values.put("usetphone", "");
        // values.put("useravatar", "");

        values.put("park_id", park_id);
        values.put("owner_id", owner_id);
        values.put("parkname", parkname);
        values.put("abstrac", abstrac);
        values.put("province", province);
        values.put("city", city);
        values.put("district", district);
        values.put("location", location);
        values.put("balance", balance);
        values.put("frozen_amount", frozen_amount);
        values.put("pending_amount", pending_amount);
        values.put("sp_amount", sp_amount);
        values.put("status", status);
        values.put("created_at", created_at);
        values.put("updated_at", updated_at);
        values.put("logo", logo);

        values.put("rolename", rolename);
        values.put("is_system_role", is_system_role);

        values.put("user_id", user_id);
        values.put("join_time", join_time);

        return db.insert("parkinfo", null, values);

    }

    public ArrayList<ParkInfoBean> query() {
        ArrayList<ParkInfoBean> list = new ArrayList<ParkInfoBean>();
        //查询获得游标
        Cursor cursor = db.query("parkinfo", null, null, null, null, null, null, null);

        //判断游标是否为空
//		if(cursor.moveToFirst()){

        //一到下一条记录
        while (cursor.moveToNext()) {
            int parkid = cursor.getColumnIndex("park_id");
            String park_id = cursor.getString(parkid);

            int par_kname = cursor.getColumnIndex("parkname");
            String parkname = cursor.getString(par_kname);
            list.add(new ParkInfoBean(park_id, parkname));
        }
        cursor.close();
//		}
        return list;

    }

    public void delete() {
        if (db != null) {
            db.delete("parkinfo", null, null);
        }

    }

}
